

package net.betou.trade.main.manager.impl;


import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Suggestion;
import net.betou.trade.entity.main.SuggestionType;
import net.betou.trade.main.dao.SuggestionDao;
import net.betou.trade.main.manager.SuggestionMng;
import net.betou.trade.role.setting.SuggestionFinderConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author O.Y.
 */
@Service
@Transactional
public class SuggestionMngImpl implements SuggestionMng{
	
	private final Logger log = LoggerFactory.getLogger( this.getClass() );

	public Suggestion deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	public Suggestion [ ] deleteByIds( Integer [ ] ids ) {
		Suggestion [ ] beans = new Suggestion [ ids.length ];

		for ( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}

	@Transactional( readOnly = true )
	public Suggestion findById( Integer id ) {
		return dao.findById( id );
	}

	@Transactional( readOnly = true )
	public Suggestion findMySuggestion( Integer id, Integer adminId, boolean system ) {
		if( system ){
			return dao.findById( id );
		}
		
		return dao.findMySuggestion( id, adminId );
	}

	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize, String filter ) {
		boolean system = admin.getSystem( );

		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, null, null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, filter );
		}else{
			SuggestionFinderConfig config = new SuggestionFinderConfig( admin );
			String deep = config.getDeepHql();
			log.debug(deep);
			String ti = admin.getTopIntermedia();
			if(ti==null || ti.length()==0 || ti.equals("0")){
				ti = admin.getId().toString();
			}else{
				ti = ti + "," + admin.getId().toString();
			}
			pagination = dao.getPage( admin.getId( ), ti, deep, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, filter );
			
		}
		
		return pagination;
		
	}

	public Suggestion save( Suggestion bean, AgentAndAdmins admin, SuggestionType st) {
		bean.init();
		bean.setAdmin( admin );
		bean.setType( st );
		return dao.save( bean );
	}

	public Suggestion updateStatus( Suggestion bean ) {
		
		Updater< Suggestion > updater = new Updater< Suggestion >( bean, Updater.UpdateMode.MIN );
		
		updater.include( "status" ); 

		return dao.updateByUpdater( updater );
	}
	
	@Autowired
	private SuggestionDao dao;

}
